;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Logistics.FeeruleHY", [])
        .controller("FeeruleHYCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "$timeout", "$filter", function ($scope, ybWidget, ybHttp, MESG_TYPE, UserContextService, $timeout, $filter) {
            $scope.tabFlag = {};
            var tabFlag = {
                0: "新增",
                1: "编辑"
            };

            // 列表查询条件
            $scope.condition = {};
            // 表格列配置信息
            $scope.toolbarConfig = {};
            // 表格信息
            $scope.tableConfig = {
                fixedWidth: 150,
                scrollWidth: 2400,
                tableCol: [
                    {
                        name: "省份",
                        dataField: "province",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "省份编码",
                        dataField: "provinceCode",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "城市",
                        dataField: "city",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "城市编码",
                        dataField: "cityCode",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "是否主站",
                        dataField: "isMainShow",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "标段-线路",
                        dataField: "sectionLine",
                        width: 120,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "备件中心",
                        dataField: "center",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "里程(公里)",
                        dataField: "distance",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "配载运输时间(天)",
                        dataField: "time",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "重量<200公斤(元/公斤)",
                        dataField: "weight1",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "200公斤≤重量<500公斤(元/公斤)",
                        dataField: "weight2",
                        width: 160,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "500公斤≤重量<2吨(元/吨)",
                        dataField: "weight3",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "2吨≤重量<5吨(元/吨)",
                        dataField: "weight4",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "5吨≤重量<10吨(元/吨)",
                        dataField: "weight5",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "重量≥10吨(元/吨)",
                        dataField: "weight6",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "备注",
                        dataField: "remark",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        isShow: true,
                        isFixed: true
                    }
                ]
            };
            // 切换配置表格列展示隐藏
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                    return;
                }
                $timeout(function () {
                    $scope.toolbarConfig["show" + toolbar] = true;
                });
            };
            // 设置表格宽度
            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: { left: $scope.tableConfig.fixedWidth + "px" }
                };
            };
            // 当前选中行
            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.markInfo = {
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.dataList = [];

            $scope.search = function (curPage) {
                if (curPage) $scope.pageInfo.curPage = curPage;
                getDataList();
            };
            function getDataList() {
                var url = '/hylogisticsfeerule/list';

                var param = {
                    filters: angular.copy($scope.condition),
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                };
                ybHttp.Post('device', url, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    if (!res.value) return;

                    angular.forEach(res.value.list, function (item) {
                        item.isMainShow = $filter("TrueOrFalseOrNull")(item.isMain);
                        item.createDateShow = $filter("YbDate")(item.createDate, 'yyyy-MM-dd HH:mm:ss');
                    });

                    $scope.pageInfo.totalRows = res.value.total;
                    $scope.pageInfo.totalPages = res.value.pages;
                    $scope.dataList = res.value.list;
                });
            }


            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.detailInfo = { isMain: 0 };

            // 切换标签页
            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        $scope.detailInfo = { isMain: 0 };
                        break;
                    default:
                        break;
                }
            };

            $scope.addData = function () {
                $scope.tabFlag.type = 0;
                $scope.tabFlag.name = tabFlag[0];
                $scope.changeTab(1);
            };

            $scope.editData = function (item) {
                $scope.tabFlag.type = 1;
                $scope.tabFlag.name = tabFlag[1];
                $scope.detailInfo = JSON.parse(JSON.stringify(item));
                $scope.changeTab(1);
            };

            $scope.delData = function (item) {
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function (data) {
                    var url = '/hylogisticsfeerule/remove';
                    ybHttp.Post('device', url, [item.id]).then(function (res) {
                        if (res.code !== 0) {
                            ybWidget.ShowMessage(res, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        $scope.search(1);
                    });
                })
            };

            // 保存
            $scope.saveData = UserContextService.Debounce(function (argu) {
                saveDataFunc(argu);
            }, 500);

            function saveDataFunc (valid) {
                if (!valid) {
                    $scope.submitted = true;
                    return;
                }
                var url = $scope.tabFlag.type == 0 ? '/hylogisticsfeerule/add' : '/hylogisticsfeerule/update';
                var param = angular.extend($scope.detailInfo);
                ybHttp.Post('device', url, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.search(1);
                    $scope.changeTab(0);
                });
            };

            // 取消
            $scope.cancel = function () {
                $scope.changeTab(0);
            };

            $scope.setMainCity = function({id}) {
                ybHttp.Post('device', '/hylogisticsfeerule/updateMainCity', {id}).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.search(1);
                });
            };

            // 导入
            $scope.importData = function () {
                var param = {
                    name: "对账信息",
                    templateReqService: "device",
                    templateReqUrl: "/hylogisticsfeerule/downloadExcel",
                    importReqService: "device",
                    importReqUrl: "/hylogisticsfeerule/importExcel",
                    importErrorUrl: ""
                };
                ybWidget.ImportFile(param).then(function (data) {
                    $scope.search(1);
                });
            };

            // 导出
            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/hylogisticsfeerule/exportAll")
            };

            // 初始化页面信息
            (function () {
                $scope.setFixedWidth();
                $scope.search(1);
            })()
        }])
        ;
})(window, window.angular);